package com.mygame.xinyue.service;

import com.mygame.common.natsConfig.Subscribe;
import com.mygame.common.utils.TopicUtil;
import com.mygame.message.msg.GameMessagePackage;
import io.nats.streaming.Message;
import io.nats.streaming.StreamingConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class NatsService {
    //    @Subscribe(value = "bbb", durableName = "asdcdf")
//    public void asd(Message message) {
//        String s = new String(message.getData(), StandardCharsets.UTF_8);
//        System.out.println("收到消息: " + s);
//    }
    @Autowired
    private StreamingConnection sc;

//    @Subscribe(value = "gateway-game-message-topic-1001")
//    public void gameMsg(Message message) throws Exception {
//        String s = new String(message.getData(), StandardCharsets.UTF_8);
//        GameMessagePackage gameMessagePackage = GameMessageHelper.readGameMessagePackage(message.getData());
//        String topic = TopicUtil.generateTopic("gateway-game-message-topic", gameMessagePackage.getHeader().getFromServerId());// 来自那个服务器
//        byte[] value = GameMessageHelper.buildMessage(gameMessagePackage);// 向消息总线服务发布客户端请求消息。
//        sc.publish(topic,value);
//        System.out.println("收到消息: " + s);
//    }
}
